package org.sdp.pearproject.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class ThreadLocalResponse {

    private static ThreadLocal<JSONObject> responseInfo = new ThreadLocal<>();

    public static JSONObject getResponseInfo() {
        return responseInfo.get();
    }

    public void setResponseInfo(JSONObject info) {
        responseInfo.set(info);
    }

    public void set(String key, Object value) {
        JSONObject j = getResponseInfo();
        if (j == null) {
            j = new JSONObject();
        }
        if (value == null) {
            j.remove(key);
        } else {
            j.put(key, value);
        }
        setResponseInfo(j);
    }

    public Object get(String key) {
        JSONObject j = getResponseInfo();
        if (j == null) {
            return null;
        }
        return j.get(key);
    }

    public String getString(String key) {
        Object v = get(key);
        if (v == null) {
            return null;
        }
        return v + "";
    }

    public Integer getInteger(String key) {
        Object v = get(key);
        if (v == null) {
            return null;
        }
        return Integer.valueOf(v+"");
    }

}
